package com.rt.easyjava.base.biz1.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.base.biz1.bean.TmptBaseFileV1Biz;
import com.rt.easyjava.base.biz1.dto.TmptBaseFileV1BizDto;
import com.rt.easyjava.base.biz1.service.intf.TmptBaseFileV1BizService;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.base.UserDetailsDto;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.utils.others.DateTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2021-04-06
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson) or userDetailsDto.getUserId()
 */
@Slf4j
public class TmptBaseFileV1Controller {
    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected TmptBaseFileV1BizService tmptBaseFileV1BizService;

    protected Result page(TmptBaseFileV1BizDto tmptBaseFileV1BizDto, UserDetailsDto userDetailsDto){
        Result<TmptBaseFileV1BizDto> result = new Result<>();

        Page<TmptBaseFileV1Biz> page = new Page<>(tmptBaseFileV1BizDto.getPage(), tmptBaseFileV1BizDto.getLimit());

        // 查询条件
        TmptBaseFileV1Biz tmptBaseFileV1BizQuery = mapper.map(tmptBaseFileV1BizDto, TmptBaseFileV1Biz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptBaseFileV1BizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptBaseFileV1BizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<TmptBaseFileV1Biz> queryWrapper=new QueryWrapper<>(tmptBaseFileV1BizQuery);
        //特殊查询条件
        queryWrapper.ge(tmptBaseFileV1BizDto.getCreatedate_Start()!=null,"createdate",tmptBaseFileV1BizDto.getCreatedate_Start());
        queryWrapper.le(tmptBaseFileV1BizDto.getCreatedate_End()!=null,"createdate",tmptBaseFileV1BizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptBaseFileV1BizDto.getCondition(), tmptBaseFileV1BizDto.isAsc(), tmptBaseFileV1BizDto.getColumn());

        return tmptBaseFileV1BizService.page(page, queryWrapper);
    }

    /**
        树结构查询方法，非树结构会造一个空方法备用着。
    */
    protected Result tree (Long id) {
        Result<TmptBaseFileV1BizDto> result = new Result<>();
        // when id=0 , id=0 is parantId=0 , 默认设置一个空根
        TmptBaseFileV1BizDto tmptBaseFileV1BizDto = new TmptBaseFileV1BizDto();
        tmptBaseFileV1BizDto.setId(0L);
        if (id!=0) {
            TmptBaseFileV1Biz tmptBaseFileV1Biz = tmptBaseFileV1BizService.getById(id);
            tmptBaseFileV1BizDto = mapper.map(tmptBaseFileV1Biz, TmptBaseFileV1BizDto.class);
        }
        treeReduce(tmptBaseFileV1BizDto);
        result.setObj(tmptBaseFileV1BizDto);

        return result.success();
    }

    private void treeReduce(TmptBaseFileV1BizDto tmptBaseFileV1BizDto){
    }

    protected Result myPage(TmptBaseFileV1BizDto tmptBaseFileV1BizDto, UserDetailsDto userDetailsDto){
        Result<TmptBaseFileV1BizDto> result = new Result<>();

        Page<TmptBaseFileV1Biz> page = new Page<>(tmptBaseFileV1BizDto.getPage(), tmptBaseFileV1BizDto.getLimit());

        // 查询条件
        TmptBaseFileV1Biz tmptBaseFileV1BizQuery = mapper.map(tmptBaseFileV1BizDto, TmptBaseFileV1Biz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptBaseFileV1BizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptBaseFileV1BizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptBaseFileV1Biz> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptBaseFileV1BizQuery , "createdate");
        //特殊查询条件
        queryWrapper.ge(tmptBaseFileV1BizDto.getCreatedate_Start()!=null,"t1.createdate",tmptBaseFileV1BizDto.getCreatedate_Start());
        queryWrapper.le(tmptBaseFileV1BizDto.getCreatedate_End()!=null,"t1.createdate",tmptBaseFileV1BizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptBaseFileV1BizDto.getCondition(), tmptBaseFileV1BizDto.isAsc(), "t1."+tmptBaseFileV1BizDto.getColumn());

        return tmptBaseFileV1BizService.myPage(page, queryWrapper, tmptBaseFileV1BizDto.getHasFooter());
    }

    protected Result selectBy(TmptBaseFileV1BizDto tmptBaseFileV1BizDto, UserDetailsDto userDetailsDto){
        Result<TmptBaseFileV1BizDto> result = new Result<>();

        // 查询条件
        TmptBaseFileV1Biz tmptBaseFileV1BizQuery = mapper.map(tmptBaseFileV1BizDto, TmptBaseFileV1Biz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptBaseFileV1BizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptBaseFileV1BizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<TmptBaseFileV1Biz> queryWrapper=new QueryWrapper<>(tmptBaseFileV1BizQuery);
        //特殊查询条件
        queryWrapper.ge(tmptBaseFileV1BizDto.getCreatedate_Start()!=null,"createdate",tmptBaseFileV1BizDto.getCreatedate_Start());
        queryWrapper.le(tmptBaseFileV1BizDto.getCreatedate_End()!=null,"createdate",tmptBaseFileV1BizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptBaseFileV1BizDto.getCondition(), tmptBaseFileV1BizDto.isAsc(), tmptBaseFileV1BizDto.getColumn());

        result = tmptBaseFileV1BizService.selectBy(queryWrapper);

        return result;
    }

    protected Result selectById(Long id){
        return tmptBaseFileV1BizService.selectById(id);
    }

    protected Result select4column(String columnName){
        return tmptBaseFileV1BizService.select4column(columnName);
    }

    protected Result create(@RequestBody TmptBaseFileV1BizDto tmptBaseFileV1BizDto,
                    String _userId, String _userName, String _companyId){
        //插入参数
        tmptBaseFileV1BizDto.setCreatemanId(Long.parseLong(_userId));
        tmptBaseFileV1BizDto.setCreateman(_userName);
        tmptBaseFileV1BizDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptBaseFileV1BizDto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return tmptBaseFileV1BizService.create(tmptBaseFileV1BizDto);
    }

    protected Result update(@RequestBody TmptBaseFileV1BizDto tmptBaseFileV1BizDto,
                        String _userId, String _userName){
        //参数
        tmptBaseFileV1BizDto.setModifymanId(Long.parseLong(_userId));
        tmptBaseFileV1BizDto.setModifyman(_userName);
        tmptBaseFileV1BizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptBaseFileV1BizDto.setCreatedate(null);
        tmptBaseFileV1BizDto.setCreateman(null);
        tmptBaseFileV1BizDto.setCreatemanId(null);
        tmptBaseFileV1BizDto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", tmptBaseFileV1BizDto.getId()==null?"0":tmptBaseFileV1BizDto.getId());

        //调用服务
        return tmptBaseFileV1BizService.update(tmptBaseFileV1BizDto, updateWrapper);
    }

    protected Result delete(Long id, String _userId, String _userName){
        Result result = new Result();

        //参数
        TmptBaseFileV1BizDto tmptBaseFileV1BizDto = new TmptBaseFileV1BizDto();
        tmptBaseFileV1BizDto.setModifymanId(Long.parseLong(_userId));
        tmptBaseFileV1BizDto.setModifyman(_userName);
        tmptBaseFileV1BizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptBaseFileV1BizDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return tmptBaseFileV1BizService.update(tmptBaseFileV1BizDto, updateWrapper);
    }

    protected Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//tmptBaseFileV1BizService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

}
